home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 09 / hgraph2 / h_graph2.c < prev    next >
C/C++ Source or Header  |  1988-07-14  |  14KB  |  541 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <dos.h>
  4. #include <h_graph2.h>
  5. #include <mouse.h>
  6.  
  7. int m_init(buttons)
  8. int *buttons;
  9. {
  10.    union REGS reg;
  11.    reg.x.ax=0;
  12.    int86(0x33,®,®);
  13.    *buttons=reg.x.bx;
  14.    return(reg.x.ax);
  15. }
  16.  
  17. void m_cursor(on)
  18. int on;
  19. {
  20.    union REGS reg;
  21.    reg.x.ax=((on)?1:2);
  22.    int86(0x33,®,®);
  23. }
  24.  
  25. int m_status(hpos,vpos)
  26. int *hpos,*vpos;
  27. {
  28.    union REGS regs;
  29.    regs.x.ax=3 ;
  30.    int86(0x33,®s,®s);
  31.    *hpos=regs.x.cx;
  32.    *vpos=regs.x.dx;
  33.    return regs.x.bx;
  34. }
  35.  
  36. void  m_move(hpos,vpos)
  37. int   hpos,vpos;
  38. {
  39.    union REGS regs;
  40.    regs.x.ax=4;
  41.    regs.x.cx=hpos;
  42.    regs.x.dx=vpos;
  43.    int86(0x33,®s,®s);
  44. }
  45.  
  46. void  m_limit(hmin,vmin,hmax,vmax)
  47. int   hmin,hmax,vmin,vmax;
  48. {
  49.    union REGS regs;
  50.    regs.x.ax=7;
  51.    regs.x.cx=hmin;
  52.    regs.x.dx=hmax;
  53.    int86(0x33,®s,®s);
  54.    regs.x.ax=8;
  55.    regs.x.cx=vmin;
  56.    regs.x.dx=vmax;
  57.    int86(0x33,®s,®s);
  58. }
  59.  
  60. void m_shape(seg,offset)
  61. int seg,*offset;
  62. {
  63.    union REGS regs;
  64.    struct SREGS sregs;
  65.    regs.x.ax=9;
  66.    regs.x.bx=*(offset+32);     /* horiz HOTSPOT in cursor mask */
  67.    regs.x.cx=*(offset+33);     /* vert HOTSPOT in cursor mask */
  68.    regs.x.dx=offset;           /* first 16 ints are the AND mask */
  69.    sregs.es=seg;               /* next 16 ints are the XOR mask */
  70.    int86x(0x33,®s,®s,&sregs);
  71. }
  72.  
  73.  
  74. int h_isherc()
  75. {
  76.    union REGS reg;
  77.    char save_byte;
  78.    extern char far *h_p;
  79.    reg.h.ah=15;
  80.    int86(16,®,®);
  81.    if(reg.h.al!=7) return(-1);
  82.    save_byte=*(h_p+65535);
  83.    h_p=H_MEMLOC;
  84.    *(h_p+65535)=170;
  85.    if(*(h_p+65535)!=170) return(1);
  86.    *(h_p+65535)=85;
  87.    if(*(h_p+65535)!=85) return(1);
  88.    *(h_p+65535)=save_byte;
  89.    return(0);
  90. }
  91.  
  92. int h_init(mode)
  93. int mode;
  94. {
  95.    extern int h_vpage,h_apage,h_xpos,h_ypos;
  96.    extern int h_pmode,h_vmode,h_color,h_start;
  97.    extern int h_xmax,h_xmin,h_ymax,h_ymin;
  98.    extern char far *h_p;
  99.    static char text[]={97,80,82,15,25,6,25,25,2,13,11,12,0,0,0,0};
  100.    static char graph[]={53,45,46,7,91,2,87,87,2,3,0,0,0,0,0,0};
  101.    char *ptr;
  102.    unsigned i;
  103.    union REGS reg;
  104.    outp(MODEPORT,((h_vmode==99)?2:0));
  105.    ptr=((mode==99)?graph:text);
  106.    reg.h.ah=0;
  107.    reg.h.al=((mode==99)?6:7);
  108.    int86(0x10,®,®);
  109.    outp(MODEPORT,((mode==99)?2:0));
  110.    for(i=0;i<16;i++)
  111.    {
  112.       outp(ADDRPORT,i);
  113.       outp(DATAPORT,*(ptr+i));
  114.    }
  115.    h_vpage=h_apage=h_xmin=h_ymin=h_ypos=h_xpos=h_pmode=h_start=0;
  116.    h_vmode=((mode==99)?99:7);
  117.    h_xmax=719;
  118.    h_ymax=347;
  119.    if(mode==99)
  120.    {
  121.       h_mempse(h_p,0,65535);
  122.    }
  123.    h_color=1;
  124.    outp(MODEPORT,((mode==99)?10:8));
  125.    return(h_vmode);
  126. }
  127.  
  128.  
  129. int h_setvpage(page)
  130. int page;
  131. {
  132.    extern int h_vpage;
  133.    extern int h_vmode;
  134.    int old_page;
  135.    if(h_vmode==99)
  136.    {
  137.       outp(MODEPORT,10+((page==1)?128:0));
  138.    }
  139.    old_page=h_vpage;
  140.    h_vpage=page;
  141.    return(old_page);
  142. }
  143.  
  144.  
  145. void h_onoff(on)
  146. int on;
  147. {
  148.    extern int h_vmode;
  149.    extern int h_vpage;
  150.    outp(MODEPORT,((on)?8:0)+((h_vpage==1)?128:0)+((h_vmode==99)?2:0));
  151. }
  152.  
  153. void h_drawc(letter,alf_ptr)
  154. char letter;
  155. char *alf_ptr;
  156. {
  157.    extern int h_xpos;
  158.    extern int h_ypos;
  159.    extern int h_xmin;
  160.    extern int h_ymin;
  161.    extern int h_xmax;
  162.    extern int h_ymax;
  163.    extern int h_pmode;
  164.    extern int h_color;
  165.    extern char far *h_p;
  166.    int delta_x,delta_y,height,width,start,stop;
  167.    int i,offset,byte_loc,x1,y1,start_loc,stop_loc,edge_loc;
  168.    char andmask1,andmask2,this_byte,last_bit;
  169.    delta_y=(int)(*alf_ptr);
  170.    delta_x=(int)(*(alf_ptr+1));
  171.    if(delta_y & 128) delta_y=(delta_y & ~128)*-1;
  172.    if(delta_x & 128) delta_x=(delta_x & ~128)*-1;
  173.    height=(int)*(alf_ptr+2);
  174.    width=(int)*(alf_ptr+3);
  175.    start=(int)*(alf_ptr+4);
  176.    stop=(int)*(alf_ptr+5);
  177.    if(h_xpos+width<h_xmin || h_xpos>=h_xmax || h_ypos+height<h_ymin || h_ypos>h_ymax)
  178.    {
  179.       h_xpos+=(int)delta_x;
  180.       h_ypos+=(int)delta_y;
  181.       return;
  182.    }
  183.    else
  184.    {
  185.       offset=h_xpos & 7;
  186.       andmask1=(char)(255<<(8-offset));
  187.       andmask2=255>>(offset+((width==8)?0:1));
  188.       if(h_xpos>=h_xmin && h_xpos+width<=h_xmax);
  189.       else
  190.       {
  191.          if(h_xpos<=h_xmax-width)
  192.          {
  193.             start_loc=((h_xpos+16)>>3)-2;
  194.             edge_loc=h_xmin>>3;
  195.             if(start_loc==edge_loc)
  196.             {
  197.                andmask1|=(char)(255<<(8-(h_xmin & 7)));
  198.             }
  199.             else
  200.             {
  201.                andmask1=255;
  202.                andmask2|=(char)(255<<(8-(h_xmin & 7)));
  203.             }
  204.          }
  205.          else
  206.          {
  207.             stop_loc=(h_xpos+8)>>3;
  208.             edge_loc=h_xmax>>3;
  209.             if(stop_loc==edge_loc)
  210.             {
  211.                andmask2|=255>>(h_xmax & 7);
  212.             }
  213.             else
  214.             {
  215.                andmask2=255;
  216.                andmask1|=255>>(h_xmax & 7);
  217.             }
  218.          }
  219.       }
  220.    }
  221.    h_p=H_MEMLOC;
  222.    x1=h_xpos;
  223.    switch(h_pmode)
  224.    {
  225.       case H_PSET:
  226.          for(i=0;i<height;i++)
  227.          {
  228.             this_byte=*(alf_ptr+6+((int)(letter)*14)+i);
  229.             last_bit=0;
  230.             y1=h_ypos+i;
  231.             if(y1>=h_ymin && y1<h_ymax)
  232.             {
  233.                byte_loc=H_FORMULA;
  234.                *(h_p+byte_loc)=(*(h_p+byte_loc) & andmask1) | (char)((this_byte>>offset));
  235.                if(width==9)
  236.                {
  237.                   if(letter>start && letter<stop)
  238.                   {
  239.                      last_bit=this_byte & 1;
  240.                   }
  241.                   *(h_p+ ++byte_loc)=(*(h_p+byte_loc) & andmask2) | ((char)(this_byte<<(8-offset))) | ((char)(last_bit<<(7-offset)));
  242.                }
  243.                else
  244.                {
  245.                   *(h_p+ ++byte_loc)=(*(h_p+byte_loc) & andmask2) | ((char)(this_byte<<(8-offset)));
  246.                }
  247.             }
  248.          }
  249.          break;
  250.       case H_OR:
  251.          for(i=0;i<height;i++)
  252.          {
  253.             this_byte=*(alf_ptr+6+((int)(letter)*14)+i);
  254.             last_bit=0;
  255.             y1=h_ypos+i;
  256.             if(y1>=h_ymin && y1<h_ymax)
  257.             {
  258.                byte_loc=H_FORMULA;
  259.                *(h_p+byte_loc)=*(h_p+byte_loc) | (char)((this_byte>>offset));
  260.                if(width==9)
  261.                {
  262.                   if(letter>start && letter<stop)
  263.                   {
  264.                      last_bit=this_byte & 1;
  265.                   }
  266.                   *(h_p+ ++byte_loc)=*(h_p+byte_loc) | ((char)(this_byte<<(8-offset))) | ((char)(last_bit<<(7-offset)));
  267.                }
  268.                else
  269.                {
  270.                   *(h_p+ ++byte_loc)=*(h_p+byte_loc) | ((char)(this_byte<<(8-offset)));
  271.                }
  272.             }
  273.          }
  274.          break;
  275.       case H_XOR:
  276.          for(i=0;i<height;i++)
  277.          {
  278.             this_byte=*(alf_ptr+6+((int)(letter)*14)+i);
  279.             last_bit=0;
  280.             y1=h_ypos+i;
  281.             if(y1>=h_ymin && y1<h_ymax)
  282.             {
  283.                byte_loc=H_FORMULA;
  284.                *(h_p+byte_loc)=*(h_p+byte_loc) ^ (char)((this_byte>>offset));
  285.                if(width==9)
  286.                {
  287.                   if(letter>start && letter<stop)
  288.                   {
  289.                      last_bit=this_byte & 1;
  290.                   }
  291.                   *(h_p+ ++byte_loc)=*(h_p+byte_loc) ^ ((char)(this_byte<<(8-offset))) | ((char)(last_bit<<(7-offset)));
  292.                }
  293.                else
  294.                {
  295.                   *(h_p+ ++byte_loc)=*(h_p+byte_loc) ^ ((char)(this_byte<<(8-offset)));
  296.                }
  297.             }
  298.          }
  299.          break;
  300.    }
  301.    h_xpos+=(int)delta_x;
  302.    h_ypos+=(int)delta_y;
  303. }
  304.  
  305. int h_loadfont(fname,alphaptr)
  306. char *fname;
  307. char *alphaptr;
  308. {
  309.    FILE *fptr;
  310.    char *alpha;
  311.    fptr=fopen(fname,"rb");
  312.    if(fptr==0) return(-1);
  313.    fread(alphaptr,1,3590,fptr);
  314.    fclose(fptr);
  315.    return(0);
  316. }
  317.  
  318. void h_boxfill(x2,y2,tile)
  319. int x2,y2;
  320. char *tile;
  321. {
  322.    extern int h_xmin,h_xmax,h_ymin,h_ymax,h_pmode,h_xpos,h_ypos;
  323.    extern char far *h_p;
  324.    int i,x1,y1,byte_loc,whole_bytes;
  325.    char save_byte,andmask1,andmask2,temp_byte;
  326.    h_p=H_MEMLOC;
  327.    if(h_xpos>h_xmax || h_ypos>h_ymax || x2<h_xmin || y2<h_ymin) return;
  328.    x1=((h_xpos>h_xmin)?h_xpos:h_xmin);
  329.    x2=((x2<h_xmax)?x2++:h_xmax);
  330.    y1=((h_ypos>h_ymin)?h_ypos:h_ymin);
  331.    y2=((y2<h_ymax)?y2:h_ymax);
  332.    andmask1=(char)(255<<(8-(x1 & 7)));
  333.    andmask2=(char)(255>>((x2 & 7)+1));
  334.    whole_bytes=((x2>>3)-(x1>>3))-1;
  335.    switch(h_pmode)
  336.    {
  337.       case H_PSET:
  338.          for(;y1<=y2;y1++)
  339.          {
  340.             save_byte=*(tile+(y1 & 7));
  341.             byte_loc=H_FORMULA;
  342.             if(whole_bytes>=0)
  343.             {
  344.                *(h_p+byte_loc)=(*(h_p+byte_loc) & andmask1) | (save_byte & (~andmask1));
  345.                if(whole_bytes)
  346.                {
  347.                   h_mempse((h_p+byte_loc+1),save_byte,whole_bytes);
  348.                   byte_loc+=whole_bytes;
  349.                }
  350.                *(h_p+ ++byte_loc)=(*(h_p+byte_loc) & andmask2) | (save_byte & (~andmask2));
  351.             }
  352.             else
  353.             {
  354.                *(h_p+byte_loc)=(*(h_p+byte_loc) & (andmask1 ^ andmask2)) | (save_byte & ~andmask1 & ~andmask2);
  355.             }
  356.          }
  357.          break;
  358.       case H_OR:
  359.          for(;y1<=y2;y1++)
  360.          {
  361.             save_byte=*(tile+(y1 & 7));
  362.             byte_loc=H_FORMULA;
  363.             if(whole_bytes>=0)
  364.             {
  365.                *(h_p+byte_loc)|=save_byte & (~andmask1);
  366.                if(whole_bytes)
  367.                {
  368.                   h_memor(h_p+byte_loc+1,save_byte,whole_bytes);
  369.                   byte_loc+=whole_bytes;
  370.                }
  371.                *(h_p+ ++byte_loc)|=save_byte & (~andmask2);
  372.             }
  373.             else
  374.             {
  375.                *(h_p+byte_loc)|=save_byte & ~andmask1 & ~andmask2;
  376.             }
  377.          }
  378.          break;
  379.       case H_AND:
  380.          for(;y1<=y2;y1++)
  381.          {
  382.             save_byte=*(tile+(y1 & 7));
  383.             byte_loc=H_FORMULA;
  384.             if(whole_bytes>=0)
  385.             {
  386.                temp_byte=andmask1 | (save_byte & (~andmask1));
  387.                *(h_p+byte_loc)&=temp_byte;
  388.                if(whole_bytes)
  389.                {
  390.                   h_memand(h_p+byte_loc+1,save_byte,whole_bytes);
  391.                   byte_loc+=whole_bytes;
  392.                }
  393.                temp_byte=andmask2 | (save_byte & (~andmask2));
  394.                *(h_p+ ++byte_loc)&=temp_byte;
  395.             }
  396.             else
  397.             {
  398.                *(h_p+byte_loc)&=save_byte & ~andmask1 & ~andmask2;
  399.             }
  400.          }
  401.          break;
  402.       case H_XOR:
  403.          for(;y1<=y2;y1++)
  404.          {
  405.             save_byte=*(tile+(y1 & 7));
  406.             byte_loc=H_FORMULA;
  407.             if(whole_bytes>=0)
  408.             {
  409.                *(h_p+byte_loc)^=save_byte & (~andmask1);
  410.                if(whole_bytes)
  411.                {
  412.                   h_memxor(h_p+byte_loc+1,save_byte,whole_bytes);
  413.                   byte_loc+=whole_bytes;
  414.                }
  415.                *(h_p+ ++byte_loc)^=save_byte & (~andmask2);
  416.             }
  417.             else
  418.             {
  419.                *(h_p+byte_loc)^=save_byte & ~andmask1 & ~andmask2;
  420.             }
  421.          }
  422.          break;
  423.    }
  424. }
  425.  
  426. int h_moveto(x1,y1)
  427. int x1,y1;
  428. {
  429.    extern int h_xmin,h_xmax,h_ymin,h_ymax,h_xpos,h_ypos;
  430.    h_xpos=x1;
  431.    h_ypos=y1;
  432.    if(x1<=h_xmax && x1>=h_xmin && y1>=h_ymin && y1<=h_ymax) return 0;
  433.    else return 1;
  434. }
  435.  
  436. void h_box(x2,y2,tile)
  437. int x2,y2;
  438. char *tile;
  439. {
  440.    int x1,y1;
  441.    extern int h_xpos,h_ypos;
  442.    x1=h_xpos;
  443.    y1=h_ypos;
  444.    h_moveto(x1,y1);
  445.    h_boxfill(x2,y1,tile);
  446.    h_moveto(x2,y1);
  447.    h_boxfill(x2,y2,tile);
  448.    h_moveto(x1,y2);
  449.    h_boxfill(x2,y2,tile);
  450.    h_moveto(x1,y1);
  451.    h_boxfill(x1,y2,tile);
  452. }
  453.  
  454. int h_setpmode(newpmode)
  455. int newpmode;
  456. {
  457.    int savemode;
  458.    extern int h_pmode;
  459.    savemode=h_pmode;
  460.    h_pmode=newpmode;
  461.    return savemode;
  462. }
  463.  
  464. void h_clrw(tile)
  465. char *tile;
  466. {
  467.    int savex,savey,savepmode;
  468.    extern int h_xpos,h_ypos,h_pmode;
  469.    savepmode=h_pmode;
  470.    savex=h_xpos;
  471.    savey=h_ypos;
  472.    h_moveto(0,0);
  473.    h_setpmode(H_PSET);
  474.    h_boxfill(719,347,tile);
  475.    h_setpmode(savepmode);
  476.    h_moveto(savex,savey);
  477. }
  478.  
  479. void h_explode(tile)
  480. char *tile;
  481. {
  482.    double i;
  483.    int halfx,halfy,totalx,totaly;
  484.    extern int h_xmin,h_xmax,h_ymin,h_ymax;
  485.    halfx=h_xmin+((h_xmax-h_xmin)>>1);
  486.    halfy=h_ymin+((h_ymax-h_ymin)>>1);
  487.    totalx=h_xmax-h_xmin;
  488.    totaly=h_ymax-h_ymin;
  489.    for(i=.01;i<1.01;i+=.025)
  490.    {
  491.        h_moveto((int)(halfx-(totalx*i)/2),(int)(halfy-(totaly*i)/2));
  492.        h_boxfill((int)(halfx+(totalx*i)/2),(int)(halfy+(totaly*i)/2),tile);
  493.    }
  494.    h_clrw(tile);
  495. }
  496.  
  497. void h_draws(string,alphabet)
  498. char *string;
  499. char *alphabet;
  500. {
  501.    int length,i;
  502.    length=strlen(string);
  503.    for(i=0;i<length;i++)
  504.    {
  505.       h_drawc(*(string+i),alphabet);
  506.    }
  507. }
  508.  
  509. void h_patterns()
  510. {
  511.    extern char lgrey[],dgrey[],fore[],back[],nlines[],nstripes[],nrhatch[];
  512.    extern char nlhatch[],wlines[],wstripes[],wrhatch[],wlhatch[];
  513.    h_boxfill(179,115,fore);
  514.    h_moveto(180,0);
  515.    h_boxfill(359,115,lgrey);
  516.    h_moveto(360,0);
  517.    h_boxfill(539,115,nlines);
  518.    h_moveto(540,0);
  519.    h_boxfill(719,115,nstripes);
  520.    h_moveto(0,116);
  521.    h_boxfill(179,231,nrhatch);
  522.    h_moveto(180,116);
  523.    h_boxfill(359,231,nlhatch);
  524.    h_moveto(360,116);
  525.    h_boxfill(539,231,wlines);
  526.    h_moveto(540,116);
  527.    h_boxfill(719,231,wstripes);
  528.    h_moveto(0,232);
  529.    h_boxfill(179,347,wrhatch);
  530.    h_moveto(180,232);
  531.    h_boxfill(359,347,wlhatch);
  532.    h_moveto(360,232);
  533.    h_boxfill(539,347,dgrey);
  534.    h_moveto(540,232);
  535.    h_boxfill(719,347,wrhatch);
  536.    h_setpmode(H_OR);
  537.    h_boxfill(719,347,wlhatch);
  538.    h_setpmode(H_PSET);
  539. }
  540.  
  541.